<template>
  <section class="page-content">
    <div class="toolbar">
      <el-form :inline="true" :model="params">
        <el-form-item label="导语列表">
          <el-input v-model="searchData.introduction" size="small" placeholder="导语名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" icon="search" @click="onSubmit">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" size="small" icon="plus" @click="create">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      v-loading="loading" 
      :data="tableData.data"
      @sort-change="sort"
      border
      style="width: 100%"
      max-height="100%">
      <el-table-column
        prop="id"
        width="80"
        sortable
        label="ID">
      </el-table-column>
      <el-table-column
        prop="introduction"
        sortable
        label="分析导语">
      </el-table-column>
      <el-table-column
        prop="sort.name"
        sortable
        label="导语分类">
      </el-table-column>
        <el-table-column
        prop="created_at"
        sortable
        label="创建日期">
      </el-table-column>
      <el-table-column
        fixed="right"
        width="180"
        label="操作">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editRow(scope.row)"
            type="default"
            size="mini">
            编辑
          </el-button>
          <el-button
            @click.native.prevent="deleteRow(scope.row)"
            type="danger"
            size="mini">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block toolbar">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="tableData.current_page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="parseInt(tableData.per_page)"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total">
      </el-pagination>
    </div>
    <!-- dialog -->
    <el-dialog class="dialog" title="分类" :visible.sync="visible">
      <el-form :model="formData" ref="formData" :rules="formRules"  label-width="120px">
       
        <el-form-item label="分析导语" prop="introduction">
          <el-input v-model="formData.introduction" placeholder="分析导语"></el-input>
        </el-form-item>
    <el-form-item label="导语分类" prop="type">
        <el-select v-model.number="formData.type" placeholder="请选择导语分类">
          <el-option
            v-for="item in categories"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false" :disabled="editLoading">取 消</el-button>
        <el-button type="primary" :loading="editLoading" @click="handleCreateOrEdit('formData')">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
  import {generateUrl} from '@/utils/helpers'
  export default {
    data () {
      return {
        loading: false,
        tableData: {
          first_page_url: '',
          from: '',
          to: '',
          last_page: '',
          last_page_url: '',
          next_page_url: '',
          prev_page_url: '',
          path: '',
          per_page: 0,
          total: 0,
          data: []
        },
        searchData: {
          introduction: ''
        },
        params: {
          order_by: 'id',
          order_type: 'desc',
          page_size: 20,
          introduction: '',
          page: 1
        },
        // dialog
        editLoading: false,
        categories: {},
        visible: false,
        formData: {
          id: null,
          introduction: ''
        },
        formRules: {
          introduction: [
            { required: true, message: '请输入名称', trigger: 'blur' },
            { min: 1, max: 1000, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ]
        }
      }
    },
    mounted () {
      this._getData()
      this._getCategories()
    },
    watch: {
      params: {
        deep: true,
        handler (newValue) {
          this._getData()
        }
      }
    },
    methods: {
      deleteRow (row) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http.delete('/YiJingIntroduction/' + row.id).then((response) => {
            this.loading = false
            this.$message.success('删除成功')
            this._getData()
          }).catch((error) => {
            this.loading = false
            console.log(error)
          })
        })
      },
      editRow (item) {
        this.formData = Object.assign({}, item)
        this.visible = true
      },
      onSubmit () {
        this.params.introduction = this.searchData.introduction
      },
      handleSizeChange (val) {
        this.params.page_size = val
      },
      handleCurrentChange (val) {
        this.params.page = val
      },
      sort (data) {
        let orderType = ''
        if (data.order === 'ascending') {
          orderType = 'asc'
        }
        if (data.order === 'descending') {
          orderType = 'desc'
        }
        this.params.order_type = orderType
        this.params.order_by = data.prop
      },
      create () {
        this._restFormData()
        this.visible = true
      },

      handleCreateOrEdit (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$http({
              method: this.formData.id === null ? 'post' : 'put',
              url: this.formData.id === null ? '/YiJingIntroduction' : '/YiJingIntroduction/' + this.formData.id,
              data: this.formData
            }).then((response) => {
              this.$message.success(response.data.message)
              this.editLoading = false
              this.visible = false
              this._getData()
            }).catch((error) => {
              this.editLoading = false
              console.log(error)
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      _restFormData () {
        this.formData = {
          id: null,
          introduction: ''
        }
      },
      _getCategories () {
        this.$http.get('/yiJingIntroductionSort').then((response) => {
          this.categories = response.data
        }).catch((error) => {
          console.log(error)
        })
      },
      _getData () {
        let url = generateUrl('/YiJingIntroduction', this.params)
        this.loading = true
        this.$http.get(url).then((response) => {
          this.loading = false
          this.tableData = response.data
        }).catch((error) => {
          this.loading = false
          console.log(error)
        })
      }
    }
  }
</script>
<style lang="scss" scoped>
  .dialog {
    .el-select {
      width: 80%;
    }
    .el-input {
      width: 80%;
    }
    .el-textarea {
      width: 80%;
    }
  } 
</style>
